package com.app.megaprosoft.modyimcarcare;

import java.util.ArrayList;

import com.app.megaprosoft.DB.Ar;
import com.app.megaprosoft.DB.SQLiteDBClass;
import com.app.megaprosoft.DB.ServicePOS;
import com.app.megaprosoft.general.DateTime;
import com.app.megaprosoft.modyimcarcare.Carcare_Login.LoadingLogin;
import com.megaprosoft.modyimcarcare.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;


public class Carcare_StatusChange extends Activity {
	
	public static final int 	DIALOG_DOWNLOAD_PROGRESS = 0;
	public static final int 	DIALOG_DOWNLOAD_PROGRESS1 = 2;
	private ProgressDialog 		mProgressDialog;
	
	private Button  			button_cancle_back,
								button_getcar,
								button_washcar,
								button_checkcar,
								button_checkcarbyhd,
								button_checkcarbymg,
								button_recive;
	
	private TextView 			tv_serviceid,
					 			tv_idcar;
	
	private String  			CarId
								,ServiceId
								,Servicestatus
								,user_Password
								,Password;
	
	AlertDialog.Builder alertDialogBuilder;
	private AlertDialog PasswordDialog,ChangeDialog,CancleDialog;
	private ListView Carcare_ServiceListView;
	
	private Handler Handler = new Handler();
	private DateTime datetime = new DateTime();
	private AlertDialog CarSearchDialog;
	AlertDialog alertDialog;
	//Intent
	private Intent  GetCar;
	
	private CarCare_Adapter_ListService carcare_listservice_adapter;
	
	private EditText et_editText1;
	private TextView tv_company,tv_Password;
	private RadioGroup rg_check;
	private RadioButton rg_c1,rg_c2;
	private Button button_1,button_2,button_3,button_4,button_5,
					button_6,button_7,button_8,button_9,button_0,button_clrsr,button_cancle,button_enter,button_dialogcancle;
	
	private int 		MODE 				= 0;
	private int 		MODE_HEAD 			= 1;
	private int 		MODE_SUPERVISOR 	= 2;
	private Boolean  	bl_HEAD 			= false;
	private Boolean 	bl_SUPERVISOR 		= false;
	
	ServicePOS servicepos = new ServicePOS();
	Ar ar = new Ar();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.a_carcare_dialog_statuschang);
		servicepos.setContext(getApplicationContext());
		ar.setContext(getApplicationContext());
		
		Bundle bundle = getIntent().getExtras();
		if (bundle != null) {
			setCarId((String) bundle.get("CarId"));
			setServiceId((String) bundle.get("ServiceID"));
			setServicestatus((String) bundle.get("Servicestatus"));
		}
		
		
//////////////////////////////////////// Button /////////////////////////////		

		button_cancle = (Button) findViewById(R.id.a_login_button_cancle);
		button_cancle.setOnClickListener(text_cancle_back);
		button_getcar = (Button) findViewById(R.id.a_login_getcar);
		button_getcar.setOnClickListener(getcar);
		button_washcar = (Button) findViewById(R.id.a_login_washcar);
		button_washcar.setOnClickListener(washcar);
		button_checkcar = (Button) findViewById(R.id.a_login_checkcarbymg);
		button_checkcar.setOnClickListener(checkcarbymg);
//		button_checkcarbyhd = (Button) findViewById(R.id.a_login_checkcarbyhd);
//		button_checkcarbyhd.setOnClickListener(checkcarbyhd);
//		button_checkcarbymg = (Button) findViewById(R.id.a_login_checkcarbymg);
//		button_checkcarbymg.setOnClickListener(checkcarbymg);
		button_recive = (Button) findViewById(R.id.a_login_button_recive);
		button_recive.setOnClickListener(recive);
		button_dialogcancle = (Button) findViewById(R.id.a_login_checkcarcancle);
		button_dialogcancle.setOnClickListener(canclecar);
		
		tv_serviceid   = (TextView) findViewById(R.id.tv_service_id);
		tv_idcar	   = (TextView) findViewById(R.id.tv_car_id);
		tv_serviceid.setText(getServiceId());
		tv_idcar.setText(getCarId());
//		Toast.makeText(Carcare_StatusChange.this, getCarId(), Toast.LENGTH_SHORT).show();
		LayoutInflater inflater = Carcare_StatusChange.this.getLayoutInflater();
		
		PasswordDialog = new AlertDialog.Builder(Carcare_StatusChange.this)
        .setView(inflater.inflate(R.layout.a_carcare_dialog_confirmuser, null))
        .setCancelable(false)
        .create();
        
		PasswordDialog.setOnShowListener(new DialogInterface.OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog) {
				
				tv_Password = (TextView) PasswordDialog.findViewById(R.id.a_login_password);
				tv_Password.setText("");
				
				button_0 = (Button) PasswordDialog.findViewById(R.id.a_login_button0);
				button_0.setOnClickListener(text_0);
				button_1 = (Button) PasswordDialog.findViewById(R.id.a_login_button1);
				button_1.setOnClickListener(text_1);
				button_2 = (Button) PasswordDialog.findViewById(R.id.a_login_button2);
				button_2.setOnClickListener(text_2);
				button_3 = (Button) PasswordDialog.findViewById(R.id.a_login_button3);
				button_3.setOnClickListener(text_3);
				button_4 = (Button) PasswordDialog.findViewById(R.id.a_login_button4);
				button_4.setOnClickListener(text_4);
				button_5 = (Button) PasswordDialog.findViewById(R.id.a_login_button5);
				button_5.setOnClickListener(text_5);
				button_6 = (Button) PasswordDialog.findViewById(R.id.a_login_button6);
				button_6.setOnClickListener(text_6);
				button_7 = (Button) PasswordDialog.findViewById(R.id.a_login_button7);
				button_7.setOnClickListener(text_7);
				button_8 = (Button) PasswordDialog.findViewById(R.id.a_login_button8);
				button_8.setOnClickListener(text_8);
				button_9 = (Button) PasswordDialog.findViewById(R.id.a_login_button9);
				button_9.setOnClickListener(text_9);
				button_clrsr = (Button) PasswordDialog.findViewById(R.id.a_login_button_clrscr);
				button_clrsr.setOnClickListener(text_clrsr);
				button_enter = (Button) PasswordDialog.findViewById(R.id.a_login_button_enter);
				button_enter.setOnClickListener(text_enter);
				button_cancle = (Button) PasswordDialog.findViewById(R.id.a_login_button_cancle);
				button_cancle.setOnClickListener(text_cancle);
			}
		});
		
		ChangeDialog = new AlertDialog.Builder(Carcare_StatusChange.this)
        .setView(inflater.inflate(R.layout.a_carcare_check_car, null))
        .setCancelable(false)
        .setPositiveButton("ตกลง", new DialogInterface.OnClickListener(){
        	@Override
        	public void onClick(DialogInterface dialog,int which){
        		
        	}
        }).setNegativeButton("ยกเลิก", new DialogInterface.OnClickListener(){
        	@Override
        	public void onClick(DialogInterface dialog,int which){
        		
        	}
        }).create();
        
		ChangeDialog.setOnShowListener(new DialogInterface.OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog) {
				
				rg_check = (RadioGroup) ChangeDialog.findViewById(R.id.a_rg_check);
				rg_c1 = (RadioButton) ChangeDialog.findViewById(R.id.C1);
				rg_c2 = (RadioButton) ChangeDialog.findViewById(R.id.C2);
				et_editText1 = (EditText) ChangeDialog.findViewById(R.id.editText1);
				
				
				rg_c2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
						// TODO Auto-generated method stub
						et_editText1.setVisibility(View.GONE);
					}
				});
				rg_c1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
						// TODO Auto-generated method stub
						et_editText1.setVisibility(View.VISIBLE);
					}
				});
				
				Button button_OK = ChangeDialog.getButton(AlertDialog.BUTTON_POSITIVE);
				Button button_Cancle = ChangeDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
				
				button_OK.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
//						try{
//			        		String data[][] = servicepos.SelectAll_ServicePOS("", getServiceId(),"");
//				        	servicepos.UpdateServicePOSStatuschancg(  
//				        							data[0][0], 
//				        							getServiceId(), 
//				        							"รอตรวจสอบ",
//				        							DateTime.getDateTimeNow(), 
//				        							"0",
//				        							"0");
//			        	}catch(Exception e){
//			        		
//			        	}
						MODE = MODE_SUPERVISOR;
			    		new  LoadingLogin().execute();
						ChangeDialog.dismiss();
			        	finish();
			        	
					}
					
				});
				button_Cancle.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						ChangeDialog.dismiss();
					}
				});
				
			}
		});
		
		CancleDialog = new AlertDialog.Builder(Carcare_StatusChange.this)
        .setView(inflater.inflate(R.layout.a_carcare_cancle_car, null))
        .setCancelable(false)
        .setIcon(R.drawable.statuscancle)
        .setTitle("สาเหตุที่ยกเลิก")
        .setPositiveButton("ตกลง", new DialogInterface.OnClickListener(){
        	@Override
        	public void onClick(DialogInterface dialog,int which){
        		
        	}
        }).setNegativeButton("ยกเลิก", new DialogInterface.OnClickListener(){
        	@Override
        	public void onClick(DialogInterface dialog,int which){
        		
        	}
        }).create();
        
		CancleDialog.setOnShowListener(new DialogInterface.OnShowListener() {
			
			@Override
			public void onShow(DialogInterface dialog) {
				
				et_editText1 = (EditText) CancleDialog.findViewById(R.id.editText1);
				
				Button button_OK = CancleDialog.getButton(AlertDialog.BUTTON_POSITIVE);
				Button button_Cancle = CancleDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
				
				button_OK.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						try{
//							servicepos.setContext(Carcare_StatusChange.this);
			        		String data[][] = servicepos.SelectAll_ServicePOS("", getServiceId(),"");
				        	servicepos.UpdateServicePOSStatuschancg(  
				        							data[0][0], 
				        							getServiceId(), 
				        							"ยกเลิก",
				        							DateTime.getDateTimeNow(), 
				        							"0",
				        							"1");
			        	}catch(Exception e){
			        		
			        	}
						
//						MODE = MODE_SUPERVISOR;
//			    		new  LoadingLogin().execute();
						CancleDialog.dismiss();
			        	finish();
			        	
					}
					
				});
				button_Cancle.setOnClickListener(new View.OnClickListener() {
					
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						CancleDialog.dismiss();
					}
					
				});
				
			}
		});
	}
	
	private OnClickListener text_cancle_back = new OnClickListener() {
        public void onClick(View v) {
        	onBackPressed();
        }
    };
 
    private OnClickListener getcar = new OnClickListener() {
        public void onClick(View v) {
        	openAlert_getcat(v);
        }
    };
    private OnClickListener washcar = new OnClickListener() {
    	public void onClick(View v) {
    	openAlert_wash(v);
    	}
    };
    private OnClickListener canclecar = new OnClickListener() {
    	public void onClick(View v) {
    	CancleDialog.show();
    	}
    };
    private OnClickListener checkcarbyhd = new OnClickListener() {
    	public void onClick(View v) {
    		MODE = MODE_HEAD;
    		new  LoadingLogin().execute();
//    		PasswordDialog.show();
        }
    };
    
    private OnClickListener checkcarbymg = new OnClickListener() {
    	public void onClick(View v) {
    		MODE = MODE_SUPERVISOR;
    		new  LoadingLogin().execute();
        }
    };
    
    private OnClickListener recive = new OnClickListener() {
    	public void onClick(View v) {
//        	try{
//        		String data[][] = servicepos.SelectAll_ServicePOS("", getServiceId(),"");
//        		servicepos.UpdateServicePOSStatuschancg(  data[0][0], 
//	        							getServiceId(), 
//	        							"คืนรถ",
//	        							DateTime.getDateTimeNow(), 
//	        							"0",
//	        							"1");
//        	}catch(Exception e){
//        		
//        	}
//        	finish();
        	Intent newActivity = new Intent(getApplicationContext(),Carcare_Service.class);
        	String data[][];
        	String ARCODE = "";
			try {
//				servicepos.setContext(Carcare_StatusChange.this);
				data = servicepos.SelectAll_ServicePOS("", getServiceId(), "");
				newActivity.putExtra("FFUserOID", data[0][4]);// UserOID
	        	newActivity.putExtra("ArCode", data[0][3]);// ArCode
	        	newActivity.putExtra("POSDocOID", data[0][1]);// POSDocOID
	        	ARCODE = data[0][3];
	        	
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	String data2[][];
			try {
//				ar.setContext(Carcare_StatusChange.this);
				data2 = ar.SelectAll_ArFF( "",ARCODE,"");
				newActivity.putExtra("CarSize", data2[0][60]);// CarSize
	        	newActivity.putExtra("CarGroupOID", data2[0][63]);// CarGroupOID
	        	newActivity.putExtra("CarGroup", data2[0][64]);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			startActivity(newActivity);
            finish();
//    
        }
    };
    
    @Override
	public void onBackPressed() {
    	finish();
	}


	public String getCarId() {
		return CarId;
	}


	public void setCarId(String carId) {
		CarId = carId;
	}


	public String getServiceId() {
		return ServiceId;
	}


	public void setServiceId(String serviceId) {
		ServiceId = serviceId;
	}


	public String getServicestatus() {
		return Servicestatus;
	}


	public void setServicestatus(String servicestatus) {
		Servicestatus = servicestatus;
	} 
    
	
	private OnClickListener text_clrsr = new OnClickListener() {
        public void onClick(View v) {
        	tv_Password.setText("");
        }
    };
    private OnClickListener text_0 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"0");
        }
    };
    private OnClickListener text_1 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"1");
        }
    };
    private OnClickListener text_2 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"2");
        }
    };
    private OnClickListener text_3 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"3");
        }
    };
    private OnClickListener text_4 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"4");
        }
    };
    private OnClickListener text_5 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"5");
        }
    };
    private OnClickListener text_6 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"6");
        }
    };
    private OnClickListener text_7 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"7");
        }
    };
    private OnClickListener text_8 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"8");
        }
    };
    private OnClickListener text_9 = new OnClickListener() {
        public void onClick(View v) {
        	String tmp = tv_Password.getText().toString();
        	tv_Password.setText(tmp+"9");
        }
    };
    private OnClickListener text_cancle = new OnClickListener() {
        public void onClick(View v) {
        	ChangeDialog.dismiss();
        }
    };	

    private OnClickListener text_enter = new OnClickListener() {
        public void onClick(View v) {
        	user_Password = tv_Password.getText().toString();
        	new  LoadingLogin().execute();
        }
    };
    
    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("Checking.....");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            mProgressDialog.setCancelable(true);
            mProgressDialog.show();
            return mProgressDialog;
        default:
            return null;
        }
    }
    
 	//-----------AsyncTask-Loading----------------------------------//
    public class LoadingLogin extends AsyncTask<String, Void, Void> {
        @SuppressWarnings("deprecation")
		protected void onPreExecute() {
        	super.onPreExecute();
        }
        
		@Override
        protected Void doInBackground(String... params) {
			
			if(MODE == MODE_HEAD){
//				myDb.Select_FFUser(strLoginName)
				bl_HEAD = true;
			}else if(MODE == MODE_SUPERVISOR){
				bl_SUPERVISOR = true;
			}
    		return null;
        }
		
        @SuppressWarnings("deprecation")
		protected void onPostExecute(Void unused) {
        	if(bl_HEAD){
        		try{
            		String data[][] = servicepos.SelectAll_ServicePOS("", getServiceId(),"");
            		servicepos.UpdateServicePOSStatuschancg(  
            								data[0][0], 
    	        							getServiceId(), 
    	        							"เช็คเรียบร้อย",
    	        							DateTime.getDateTimeNow(), 
    	        							"0",
    	        							"0");
            		
            	}catch(Exception e){
            	}
        	}else if(bl_SUPERVISOR){
        		try{
            		String data[][] = servicepos.SelectAll_ServicePOS("", getServiceId(),"");
            		servicepos.UpdateServicePOSStatuschancg(  
            								data[0][0], 
    	        							getServiceId(), 
    	        							"รอปล่อย",
    	        							DateTime.getDateTimeNow(), 
    	        							"0",
    	        							"0");
            	}catch(Exception e){
            	}
        	}
        	
        	finish();
        } 
    }
    
    
	private void openAlert_getcat(View view) {
		 alertDialogBuilder = new AlertDialog.Builder(Carcare_StatusChange.this);
		 alertDialogBuilder.setTitle("ยืนยันการเปลี่ยนสถานะ");
		 alertDialogBuilder.setIcon(R.drawable.status3);
		 alertDialogBuilder.setMessage("ทำการเปลียนสถานะบริการเป็น 'ขึ้นล้าง'");
		 // set positive button: Yes message
		 alertDialogBuilder.setPositiveButton("ยืนยัน",new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				try{
	        		String data[][] = servicepos.SelectAll_ServicePOS("", getServiceId(),"");
	        		servicepos.UpdateServicePOSStatuschancg(  
	        								data[0][0], 
		        							getServiceId(), 
		        							"ขึ้นล้าง",
		        							DateTime.getDateTimeNow(), 
		        							"0",
		        							"0");
	        	}catch(Exception e){
	        	} 
				finish();
			}
		 });
		 alertDialogBuilder.setNegativeButton("ยกเลิก",new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,int id) {
					dialog.cancel();
				}
		 });
		 alertDialog = alertDialogBuilder.create();
		 alertDialog.show();
		 
	}
	
	private void openAlert_wash(View view) {
		 alertDialogBuilder = new AlertDialog.Builder(Carcare_StatusChange.this);
		 alertDialogBuilder.setTitle("ยืนยันการเปลี่ยนสถานะ");
		 alertDialogBuilder.setIcon(R.drawable.status2);
		 alertDialogBuilder.setMessage("ทำการเปลียนสถานะบริการเป็น 'ล้างเสร็จ'");
		 // set positive button: Yes message
		 alertDialogBuilder.setPositiveButton("ยืนยัน",new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				try{
	        		String data[][] = servicepos.SelectAll_ServicePOS("", getServiceId(),"");
	        		servicepos.UpdateServicePOSStatuschancg(  
	        								data[0][0], 
		        							getServiceId(), 
		        							"ล้างเสร็จ",
		        							DateTime.getDateTimeNow(), 
		        							"0",
		        							"0");
	        	}catch(Exception e){
	        		
	        	}
	        	finish();
	        }
		 });
		 alertDialogBuilder.setNegativeButton("ยกเลิก",new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,int id) {
					dialog.cancel();
				}
		 });
		 alertDialog = alertDialogBuilder.create();
		 alertDialog.show();
	}
	
	private void openAlert_checkcar(View view) {
		 alertDialogBuilder = new AlertDialog.Builder(Carcare_StatusChange.this);
		 alertDialogBuilder.setTitle("ยืนยันการเปลี่ยนสถานะ");
		 alertDialogBuilder.setMessage("ทำการเปลียนสถานะบริการเป็น 'รอตรวจสอบ'");
		 // set positive button: Yes message 
		 alertDialogBuilder.setNegativeButton("ยืนยัน",new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				
	        }
		 });
		 alertDialogBuilder.setPositiveButton("ยกเลิก",new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,int id) {
					dialog.cancel();
				}
		 });
		 alertDialog = alertDialogBuilder.create();
		 alertDialog.show();
	}
}
